package com.tfswx.jkgl.core.cscl;

import com.alibaba.fastjson.JSON;
import com.tfswx.jkgl.core.cscl.base.CsclBase;
import com.tfswx.jkgl.core.task.ServiceTask;
import com.tfswx.jkgl.core.task.ServiceTaskContextHolder;
import com.tfswx.jkgl.core.utils.MyScriptEngine;
import com.tfswx.jkgl.core.utils.ServiceUtil;
import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ricky
 * @date 2021/12/27
 */
@Slf4j
@Component
public class ListFile implements CsclBase {
    @Override
    public String getName() {
        return "文件列表";
    }

    @Override
    public boolean sfxycs() {
        return true;
    }

    @Override
    public Object encode(Object value, Object jmcs, String pBds, String id) {
        log.info(pBds + "需要获取文件列表");
        if (jmcs == null) {
            throw new RuntimeException("策略参数不能为空");
        }
        CsParam csParam;
        try {
            csParam = JSON.parseObject(jmcs.toString(), CsParam.class);
        } catch (Exception e) {
            throw new RuntimeException("策略参数类型", e);
        }
        // 创建后置任务
        ListTask listTask = new ListTask();
        listTask.setCsParam(csParam);
        listTask.setZybmBdsQz(pBds);
        ServiceTaskContextHolder.addTask(listTask, id);
        return value;
    }


    public static class ListTask extends ServiceTask {
        private final String serviceName = "ywwjServiceImpl";
        private final String methodName = "list";

        @Setter
        private String zybmBdsQz;

        @Setter
        private CsParam csParam;

        public Object getParam(Object dto) {
            MyScriptEngine myScriptEngine = new MyScriptEngine();
            int i = zybmBdsQz.lastIndexOf(".");
            String newZybmBds = csParam.zybmBds;
            if (i != -1) {
                String bdsQz = zybmBdsQz.substring(0, i);
                String[] split = csParam.zybmBds.split("\\+");
                for (int j = 0; j < split.length; j++) {
                    String s = split[j];
                    split[j] = bdsQz + "." + s.trim();
                }
                newZybmBds = String.join("+", split);
            }
            log.info(newZybmBds);

            String zybm = String.valueOf(myScriptEngine.eval(newZybmBds, dto));
            log.info("资源编码：" + zybm);
            Map<String, Object> map = new HashMap<>();
            map.put("zybm", zybm);
            map.put("mkbm", csParam.mkbm);
            map.put("zylxbm", csParam.zylxbm);
            return map;
        }

        @Override
        public void run(Object dto) {
            // Object o = getParam(dto);
            Object o = ServiceUtil.excuteMethod(serviceName, methodName, getParam(dto));
            MyScriptEngine myScriptEngine = new MyScriptEngine();
            myScriptEngine.put("$data", o);
            // myScriptEngine.eval("print($data)");
            myScriptEngine.eval(zybmBdsQz + "=$data", dto);
        }
    }

    @Data
    public static class CsParam {
        // {"mkbm":"","zylxbm":"","zybmBds":""}
        private String mkbm;
        private String zylxbm;
        private String zybmBds;
    }
}
